package com.hdoit.kylin.web.document.controllers;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;
import com.hdoit.kylin.logical.document.model.WeekLog;
import com.hdoit.kylin.logical.document.service.IWeekLogService;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.viewmodel.document.WeekLogSearch;
import com.hdoit.kylin.web.common.CommonWebUtils;

/**
 * @author      lufu
 * @email       
 * @createtime  2017年08月10日 14:47:36
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/document/weekLog/")
public class WeekLogController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/document/weekLog/";
    }

    private IWeekLogService weekLogService;
    public IWeekLogService getWeekLogService() {
        return weekLogService;
    }
    @Resource
    public void setWeekLogService(IWeekLogService weekLogService) {
        this.weekLogService = weekLogService;
    }
    
    private ITeacherService teacherService;
    public ITeacherService getTeacherService() {
		return teacherService;
	}
    @Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}
    
    private IStudentService studentService;

	public IStudentService getStudentService() {
		return studentService;
	}
	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="weekLogDetailed";
    private final String saveViewName="save";
    private final String simpleAddViewName="simpleAdd";
    private final String deleteViewName = "delete";
    private final String doFinishViewName="doFinish";
    private final String updatePropertyVN="updateProperty";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,WeekLogSearch search,boolean ajaxPost,HttpServletRequest request){
    	pagerModel.setPageSize(1000);
    	
    	List<WeekLog> weekLogs;
    	if (CommonWebUtils.isTeacher(getLoginModel())) {
    		weekLogs = weekLogService.queryListByTeacherId(getLoginModel().getLoginId());
    		
		} else if (CommonWebUtils.isStudent(getLoginModel())) {
			weekLogs = weekLogService.queryListByEmployeeId(getLoginModel().getLoginId());
			
		}
		else {
			weekLogs = weekLogService.queryList();
		}

        WeekLog weekLog=new WeekLog();

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("weekLogs", weekLogs);
        model.addAttribute("weekLogSearch", search);
        model.addAttribute("weekLog", weekLog);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String weekLogDetailed(Model model,String key){
        WeekLog weekLog=new WeekLog();
        if(!StringUtils.isEmpty(key)){
            weekLog=weekLogService.queryModel(key);
        }
        model.addAttribute("weekLog", weekLog);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,WeekLog weekLog){
        if(StringUtils.isEmpty(weekLog.getId())){
            weekLog.setId(CommonUtils.getKeyId());
            weekLog.setCreateDate(DateUtils.getCurrent());
            weekLog.setCreateUserId(getLoginModel().getLoginId());
            weekLog.setCreateUserName(getLoginModel().getLoginName());
            weekLog.setModelStatus(ModelStatus.add);
        }else{
            weekLog.setModifyDate(DateUtils.getCurrent());
            weekLog.setModifyUserId(getLoginModel().getLoginId());
            weekLog.setModelStatus(ModelStatus.update);
            if(CommonWebUtils.isTeacher(getLoginModel())){
            	weekLog.setCommentMark(YesOrNoInteger.yes);
            }
        }

        ActionResult actionResult;
        if(weekLogService.applyChange(weekLog)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("weekLog", weekLog);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(weekLogService.deleteModel(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
    @RequestMapping(value = simpleAddViewName, method = { RequestMethod.POST,RequestMethod.GET},produces="text/plain;charset=utf-8")
    @ResponseBody
    public String simpleAdd(Model model,WeekLog weekLog) {
    	weekLog.setId(CommonUtils.getKeyId());
    	weekLog.setCreateDate(DateUtils.getCurrent());
    	weekLog.setCreateUserId(getLoginModel().getLoginId());
    	weekLog.setCreateUserName(getLoginModel().getLoginName());
    	weekLog.setModelStatus(ModelStatus.add);
    	
    	JsonActionResult actionResult;
    	if (weekLogService.applyChange(weekLog)) {
    		actionResult=new JsonActionResult(ManageContainer.SUCCESS);
		} else {
			actionResult=new JsonActionResult(ManageContainer.ERROR);
		}
    	
    	return actionResult.toJson();
    }
    
    @RequestMapping(value = updatePropertyVN, method = { RequestMethod.POST,RequestMethod.GET},produces="text/plain;charset=utf-8")
    @ResponseBody
    public String updateProperty(String propertyName,String value,String key){
    	if(StringUtils.isEmpty(key)||StringUtils.isEmpty(propertyName)){
    		return new JsonActionResult(ManageContainer.ERROR).toJson();
    	}
    	WeekLog weekLog=weekLogService.queryModel(key);
    	JsonActionResult actionResult;
    	if(weekLogService.updateModelSelective(weekLog)){
            actionResult=new JsonActionResult(ManageContainer.SUCCESS);
        }else{
            actionResult=new JsonActionResult(ManageContainer.ERROR);
        }
    	return actionResult.toJson();
    }
    
    @RequestMapping(value = doFinishViewName, method = { RequestMethod.POST},produces="text/plain;charset=utf-8")
    @ResponseBody
    public String doFinish(String taskId){
    	WeekLog weekLog=weekLogService.queryModel(taskId);
    	
    	if(weekLogService.updateModelSelective(weekLog)){
    		return new JsonActionResult(ManageContainer.SUCCESS).toJson();
    	}
    	return new JsonActionResult(ManageContainer.ERROR).toJson();
    }

}